package com.rent.ordersinfo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;

import com.rent.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rent.ordersinfo.mapper.RentOrdersMapper;
import com.rent.ordersinfo.domain.RentOrders;
import com.rent.ordersinfo.service.IRentOrdersService;

/**
 * 订单管理Service业务层处理
 *
 * @author cooper
 * @date 2024-10-15
 */
@Service
public class RentOrdersServiceImpl extends ServiceImpl<RentOrdersMapper, RentOrders> implements IRentOrdersService {
    @Autowired
    private RentOrdersMapper rentOrdersMapper;

    /**
     * 查询订单管理
     *
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Override
    public RentOrders selectRentOrdersByOrderId(Long orderId) {
        return rentOrdersMapper.selectRentOrdersByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     *
     * @param rentOrders 订单管理
     * @return 订单管理
     */
    @Override
    public List<RentOrders> selectRentOrdersList(RentOrders rentOrders) {
        return rentOrdersMapper.selectRentOrdersList(rentOrders);
    }

    /**
     * 新增订单管理
     *
     * @param rentOrders 订单管理
     * @return 结果
     */
    @Override
    public int insertRentOrders(RentOrders rentOrders) {
        rentOrders.setCreateTime(DateUtils.getNowDate());
        rentOrders.setOrderNumber(generateOrderNumber());
        return rentOrdersMapper.insertRentOrders(rentOrders);
    }

    /**
     * 修改订单管理
     *
     * @param rentOrders 订单管理
     * @return 结果
     */
    @Override
    public int updateRentOrders(RentOrders rentOrders) {
        return rentOrdersMapper.updateRentOrders(rentOrders);
    }

    /**
     * 批量删除订单管理
     *
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteRentOrdersByOrderIds(Long[] orderIds) {
        return rentOrdersMapper.deleteRentOrdersByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     *
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteRentOrdersByOrderId(Long orderId) {
        return rentOrdersMapper.deleteRentOrdersByOrderId(orderId);
    }

    //生成订单编号
    private String generateOrderNumber() {
        String prefix = "RO"; // RO 代表 Rent Order
        String dateStr = DateUtils.dateTimeNow("yyyyMMdd");
        String randomStr = String.format("%04d", (int) (Math.random() * 10000));
        return prefix + dateStr + randomStr;
    }
}